<template>
  <div class="login-container">
    <div class="title-container">
      <h3 class="title">{{title}}</h3>
    </div>
    <div class="login-form">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules"  auto-complete="on" label-position="left">
        <el-form-item prop="username">
          <span class="svg-container">
            <i class="el-icon-user" />
          </span>
          <!-- auto-complete="on" -->
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="用户名"
            name="username"
            type="text"
            tabindex="1"
            maxlength="10"
            show-word-limit
          />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <i class="el-icon-lock" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="密码"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <el-tooltip 
              :content="passwordType === 'password' ? '闭眼' : '显示'" 
              placement="top" 
              effect="light"
            >
              <i class="iconfont" :class="passwordType === 'password' ? 'icon-eye-close' : 'icon-eye-open'" />
            </el-tooltip>
          </span>
        </el-form-item>
        <div class="btn-operation">
          <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登入</el-button>
        </div>

      </el-form>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: "请输入用户名" }],
        password: [{ required: true, trigger: 'blur', message: "请输入密码" }]
      },
      loading: false,
      passwordType: 'password',
    }
  },
  computed: {
    ...mapGetters(['title']),
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: '/' })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  min-height: 100%;
  width: 100%;
  background-color: #0d2138;
  overflow-x: hidden;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;

  .login-form {
    width: 520px;
    max-width: 100%;
    padding: 16px;
    margin: 0 auto;
    // overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: #889aa4;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    // position: relative;
    margin-bottom: 2%;
    .title {
      font-size: 26px;
      color: #eee;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: #889aa4;
    cursor: pointer;
    user-select: none;
  }

  .btn-operation {
    width: 100%;
    margin-top: 32px;
  }
}
</style>